/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.diego.games.sudoku;

import br.diego.game.sudoku.Sudoku;
import br.diego.game.sudoku.dao.DAO;
import java.io.IOException;
import java.util.Date;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

/**
 * @author diego
 */
public class SudokuGameCanvas extends GameCanvas implements CommandListener {

    public static final String SALVE = "Salve";
    protected static final String EXIT = "Exit";
    private LayerManager lm;
    private Sudoku sudoku;
    private TiledLayer layer;
    private Command salveCommand;
    private Command exitCommand;
    private Display display;
    private Displayable displayable;

    public Command getExitCommand() {
        if (exitCommand == null) {
            exitCommand = new Command(EXIT, Command.ITEM, 0);
        }
        return exitCommand;
    }

    public Command getSalveCommand() {
        if (salveCommand == null) {
            salveCommand = new Command(SALVE, Command.ITEM, 0);
        }
        return salveCommand;
    }

    public SudokuGameCanvas(Sudoku sudoku, Display display, Displayable displayable) {
        super(true);
        try {
            this.sudoku = sudoku;
            this.x = sudoku.getX();
            this.y = sudoku.getY();
            this.display = display;
            this.displayable = displayable;
            this.setFullScreenMode(true);
            this.init();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    final static int[][] tiles1 = {
        {18, 13, 19, 10, 19, 11, 19, 14, 17},
        {15, 19, 19, 19, 14, 19, 19, 19, 13},
        {19, 19, 11, 13, 19, 12, 10, 19, 19},
        {19, 11, 19, 19, 19, 19, 19, 15, 19},
        {14, 19, 17, 19, 11, 19, 13, 19, 10},
        {19, 15, 19, 19, 19, 19, 19, 17, 19},
        {19, 19, 10, 15, 19, 17, 16, 19, 19},
        {16, 19, 19, 19, 13, 19, 19, 19, 12},
        {13, 12, 19, 14, 19, 18, 19, 10, 11}
    };
    final static int[][] tiles2 = {
        {19, 13, 19, 19, 19, 19, 19, 17, 19},
        {18, 19, 19, 19, 13, 19, 19, 12, 19},
        {19, 19, 19, 19, 10, 17, 14, 19, 18},
        {19, 19, 19, 19, 19, 14, 13, 11, 19},
        {19, 19, 19, 19, 19, 19, 19, 19, 19},
        {19, 10, 18, 17, 19, 19, 19, 19, 19},
        {15, 19, 10, 16, 12, 19, 19, 19, 19},
        {12, 14, 19, 19, 15, 19, 19, 19, 16},
        {19, 11, 19, 19, 19, 19, 19, 10, 19}
    };

    public SudokuGameCanvas(Display display, Displayable displayable) {
        this(new Sudoku(((new Date()).hashCode() % 2) == 0 ? tiles2 : tiles1), display, displayable);

    }

    private SudokuGameCanvas(boolean suppressKeyEvents) {
        super(suppressKeyEvents);
    }

    protected void pointerPressed(int x, int y) {
        super.pointerPressed(x, y);
        System.out.println("x=" + x + "y=" + y);
    }
    private int x = 0;
    private int y = 0;

    public int getGameAction(int keyCode) {
        if ((getKeyStates()) == 0) {
            keyPressed(keyCode);
        }
        return super.getGameAction(keyCode);
    }

    protected void keyReleased(int keyCode) {
        super.keyReleased(keyCode);
    }

    protected void keyPressed(int keyCode) {

        System.out.println("getkeyCode " + keyCode + "getKeyStates " + getKeyStates());
        super.keyPressed(keyCode);

        switch (keyCode) {
            case -4:
                sudoku.setXY(x, ++y);
                break;
            case -3:
                sudoku.setXY(x, --y);
                break;
            case -2:
                sudoku.setXY(++x, y);
                break;
            case -1:
                sudoku.setXY(--x, y);
                break;
            case KEY_NUM0:
            case KEY_NUM1:
            case KEY_NUM2:
            case KEY_NUM3:
            case KEY_NUM4:
            case KEY_NUM5:
            case KEY_NUM6:
            case KEY_NUM7:
            case KEY_NUM8:
            case KEY_NUM9:
                final int keyNumber = keyCode - 48;
                sudoku.setCell(keyNumber);
                break;


        }
        updateLayerManager();
    }

    public void run() {

        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }

        }
    }

    public void switchDisplayable(Displayable nextDisplayable) {
        display.setCurrent(nextDisplayable);
    }

    public void updateLayerManager() {
        Graphics g = getGraphics();
        final int[][] tabuleiro = this.sudoku.getTabuleiro();
        int mCol = tabuleiro.length;
        int mRow = tabuleiro[0].length;
        for (int row = 0; row < mRow; row++) {
            for (int col = 0; col < mCol; col++) {
                layer.setCell(col, row, tabuleiro[row][col]);
            }
        }

        layer.setPosition(16, 16);
        layer.setVisible(true);
        this.layer.paint(g);
        this.repaint();
        this.serviceRepaints();

    }

    private void init() throws IOException {

        this.lm = new LayerManager();
        layer = new TiledLayer(9, 9, Constant.getImage(), Constant.HEIGHT, Constant.WIDTH);
        lm.append(layer);
        this.setCommandListener(this);
        this.addCommand(getSalveCommand());
        this.addCommand(getExitCommand());
        updateLayerManager();
    }

    public void commandAction(Command c, Displayable d) {
        if (SALVE.equals(c.getLabel())) {
            DAO dao = DAO.getInstance();
            if (dao.getSize() < 1) {
                dao.add(sudoku);
            } else {
                dao.udpate(1, sudoku);
            }

        } else if (EXIT.equals(c.getLabel())) {
            switchDisplayable(displayable);
        }


    }
}